package Java7_Concurrency_Cookbook.concurrent_custom.custom_lock;

import java.util.concurrent.TimeUnit;

/**
 * <b>实现定制Lock类</b><br/>
 * 本示例中, MyLock类主要是利用AbstractQueuedSynchronizer的特性来实现加锁.
 * @author lixiangke
 * @date 2017/11/28
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        MyLock myLock = new MyLock();
        for (int i = 0; i < 10; i++) {
            Task task = new Task("Zoom-" + i, myLock);
            Thread thread = new Thread(task);
            thread.start();
        }

        boolean value = false;
        do {
            value = myLock.tryLock(10, TimeUnit.SECONDS);
            if (!value) {
                // System.out.printf("Main: Trying to get the Lock\n");
            }
        } while (!value);
        System.out.printf("Main: Got the lock\n");
        myLock.unlock();
        System.out.println("Main: End of the program.");
    }
}
